package yxy.game.sdk.jh;

import com.alibaba.fastjson.JSONObject;

import yxy.apple.http.server.business.BusinessType;
import yxy.apple.logger.Logger;
import yxy.game.bean.LoginOtherParm;
import yxy.game.login.Error;
import yxy.game.login.LoginModule;

@BusinessType(code = "LOGIN")
public class Login extends Sdk_jh {

	static final Logger logger = Logger.create(Login.class);
	
	@Override
	public JSONObject handle(JSONObject request) throws Exception {
		
		System.out.println(request.toString());
//		JSONObject result = new JSONObject();
		logger.info("sdk_jh:" + request);
		
//		APPID:A6AB29C0703493A9A615EB8E41610607C
//		OPENKEY:918e6a95e7ff532a7db0794674d1c535
		
//		userName, uid, tel 三个参数选填一个即可，不能同时为空。 
//		参数 		类型 		必填 	备注 
//		appID 		String 		Y 		客户申请的应用ID 		
//		userName 	String 		N 		用户名称 
//		tel 		String 		N 		用户手机号 
//		uid 		String 		N 		用户 id
		
		JSONObject datajson = request.getJSONObject("datajson");
		String uid = datajson.getString("uid");
		String subchannel = datajson.getString("subchannel");
		
//		String appId = "A6AB29C0703493A9A615EB8E41610607C";
//		
//		JSONObject postjson = new JSONObject();
//		postjson.put("appId", appId);
//		postjson.put("uid", uid);
		
//		logger.info("sdk_jh postjson:" + postjson);
		
//		JSONObject retjson = HttpUtil.response(chexklogin, 30000, postjson);
		
//		logger.info("sdk_jh ret:" + retjson);
//		if(retjson == null || retjson.equals("")) {
//			logger.info("sdk_jh ret is null");
//			return result(result, Error.Err_渠道代码错误);
//		}
		
//		JSONObject retjson = JSON.parseObject(ret);
//		int statusCode = retjson.getIntValue("statusCode");
//		if(statusCode != success) {
//			logger.info("sdk_jh statusCode is not 200");
//			return result(result, Error.Err_渠道代码错误);
//		}
		
//		JSONObject contentjson = retjson.getJSONObject("content");
//		if(!contentjson.getBooleanValue("isUserExit")) {
//			logger.info("sdk_jh isUserExit is true");
//			return result(result, Error.Err_渠道代码错误);
//		}
		
		String username = request.getString("username");
		String password = request.getString("password");
		String channel = name();
		
		String ip = request.getString("ip");
		int clusterId = request.getInteger("clusterId");

		LoginOtherParm loginOtherParm = new LoginOtherParm();
		loginOtherParm.setClusterId(clusterId);
		return new LoginModule().login(username, password, channel, subchannel, ip, loginOtherParm);
	}

	public JSONObject result(JSONObject result, Error errcode) {
		result.put("result", errcode.number());
		return result;
	}
	
//	public static void main(String[] str) {
//		String appId = "A6AB29C0703493A9A615EB8E41610607C";
//		
//		JSONObject postjson = new JSONObject();
//		postjson.put("appId", appId);
//		postjson.put("uid", 33333333);
//		
//		logger.info("sdk_jh postjson:" + postjson);
//		
//		try {
//			JSONObject retjson = HttpUtil.response(chexklogin, 30000, postjson);
//			System.out.println(retjson);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
